package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.qf.dao.TbUserRepository;

import com.qf.dao.TbUserRepository;

import com.qf.pojo.entity.TbUser;
import com.qf.pojo.request.TbUserReq;
import com.qf.pojo.response.BaseResponse;
import com.qf.service.TbUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class TbUserServiceImpl implements TbUserService {
    @Resource
    TbUserRepository tbUserRepository;

    @Override
    public BaseResponse findAll(Integer page, Integer size) {
        PageRequest of = PageRequest.of(page - 1, size);
        Page<TbUser> all = tbUserRepository.findAll(of);
        //getContent()返回结果集  getTotalElements()数据条数
        return new BaseResponse(1, "SUCCESS", all.getContent(), all.getTotalElements());
    }

    /**
     * save:新增(无id)
     * update:修改(有id)
     *
     * @param tbUserReq
     * @return
     */
    @Override
    public BaseResponse saveOrUpdate(TbUserReq tbUserReq) {
        TbUser tbUser = new TbUser();
        BeanUtils.copyProperties(tbUserReq, tbUser);
        if (tbUserReq.getUid() == null) {
            //执行新增方法
            tbUserRepository.save(tbUser);
        } else {
            //执行修改方法
            tbUserRepository.saveAndFlush(tbUser);
        }
        return new BaseResponse().SUCCESS();

    }

    /**
     * 删除
     *
     * @param uid
     * @return
     */
    @Override
    public BaseResponse delById(Integer uid) {
        tbUserRepository.deleteById(uid);

        return null;
    }

}
